/**
 * Created by zhangxiaoyu on 16/12/06.
 */
angular.module('hyt.addOrderService', ['ngResource'])

  .factory('AddOrder', function($resource, $rootScope,ENV,$ionicLoading,$state) {
    var ApiUrl = ENV.postUrl+ENV.api.addOrder,
      result;

    var resource = $resource(ApiUrl);
    return{
      getAddOrder : function(addOrder){
        console.log(addOrder);
        return resource.save({
          "customerId":addOrder.customerId, //用户id
          "belongId":addOrder.belongId, //物流公司
          "companyName":addOrder.companyName, //公司名字
          "orderId":addOrder.orderId, //订单ID，为空是新增订单，不为空是编辑订单
          "orderType":addOrder.orderType,
          "consignorName":addOrder.consignorName, //发货人姓名
          "consignorPhone":addOrder.consignorPhone, //发货人电话
          "consignorProvince":addOrder.consignorProvince,//发货人省id
          "consignorCity":addOrder.consignorCity,//发货人市id
          "consignorDistrict":addOrder.consignorDistrict,//发货人区id
          "consignorAddress":addOrder.consignorAddress, //发货人地址
          "consigneeName":addOrder.consigneeName, //收货人名称
          "consigneePhone":addOrder.consigneePhone, //收货人电话
          "consigneeProvince":addOrder.consigneeProvince,//收货人省id
          "consigneeCity":addOrder.consigneeCity,//收货人市id
          "consigneeDistrict":addOrder.consigneeDistrict,//收货人区id
          "consigneeAddress":addOrder.consigneeAddress, //收货人地址
          "deliveryType":addOrder.deliveryType, //送货方式
          "paymentSwitch":addOrder.paymentSwitch, //付款方式
          "receiptSwitch":addOrder.receiptSwitch, //是否签回单
          "pickupSwitch":addOrder.pickupSwitch, //是否提货
          "insureSwitch":addOrder.insureSwitch, //是否投保
          "remark":addOrder.remark, //备注
          "pictureInfo":addOrder.pictureInfo,
          "costMap":addOrder.costMap,
          "goodsInfo":addOrder.goodsInfo,
          "userMsg":addOrder.userMsg
        },function(r){
          console.log(r);
          if (r.msg!="0003"){
            result=r;
          }else {
            $ionicLoading.show({
              noBackdrop: true,
              template: "您的账号已在其他设备登录",
              duration: 1500
            });
            $state.go('login');
          }
          $rootScope.$broadcast('AddOrder.Updated');
        })
      },
      getResult:function(){
        return result;
      }
    }
  })
  .factory('DefaultAddress', function($resource, $rootScope,ENV,$ionicLoading,$state) {
    var ApiUrl = ENV.postUrl+ENV.api.getDefaultAddress,
      result;

    var resource = $resource(ApiUrl+':defaultAddress',{},{
      query :{
        method : 'get',
        isArray : false
      }
    });
    return{
      getDefaultAddress : function(defaultAddress){
        return resource.query({
          defaultAddress:defaultAddress
        },function(r){
          console.log(r);
          if (r.msg!="0003"){
            result=r;
          }else {
            $ionicLoading.show({
              noBackdrop: true,
              template: "您的账号已在其他设备登录",
              duration: 1500
            });
            $state.go('login');
          }
          $rootScope.$broadcast('DefaultAddress.Updated');
        })
      },
      getResult:function(){
        return result;
      }
    }
  })
  .factory('PaymentMethod', function($resource, $rootScope,ENV,$ionicLoading,$state) {
    var ApiUrl = ENV.postUrl+ENV.api.getPaymentMethod,
      result;

    var resource = $resource(ApiUrl+':paymentMethod',{},{
      query :{
        method : 'get',
        isArray : false
      }
    });
    return{
      getPaymentMethod : function(paymentMethod){
        return resource.query({
          paymentMethod:paymentMethod
        },function(r){
          console.log(r);
          if (r.msg!="0003"){
            result=r;
          }else {
            $ionicLoading.show({
              noBackdrop: true,
              template: "您的账号已在其他设备登录",
              duration: 1500
            });
            $state.go('login');
          }
          $rootScope.$broadcast('PaymentMethod.Updated');
        })
      },
      getResult:function(){
        return result;
      }
    }
  })
  .factory('PackType', function($resource, $rootScope,ENV,$ionicLoading,$state) {
    var ApiUrl = ENV.postUrl+ENV.api.getPaymentMethod,
      result;

    var resource = $resource(ApiUrl+':packType',{},{
      query :{
        method : 'get',
        isArray : false
      }
    });
    return{
      getPackType : function(packType){
        return resource.query({
          packType:packType
        },function(r){
          console.log(r);
          if (r.msg!="0003"){
            result=r;
          }else {
            $ionicLoading.show({
              noBackdrop: true,
              template: "您的账号已在其他设备登录",
              duration: 1500
            });
            $state.go('login');
          }
          $rootScope.$broadcast('PackType.Updated');
        })
      },
      getResult:function(){
        return result;
      }
    }
  })
  .factory('DeptByDivision', function($resource, $rootScope,ENV,$ionicLoading,$state) {
    var ApiUrl = ENV.postUrl+ENV.api.getDeptByDivision,
      result;

    var resource = $resource(ApiUrl+':deptByDivision',{},{
      query :{
        method : 'get',
        isArray : false
      }
    });
    return{
      getDeptByDivision : function(deptByDivision){
        return resource.query({
          deptByDivision:deptByDivision
        },function(r){
          console.log(r);
          if (r.msg!="0003"){
            result=r;
          }else {
            $ionicLoading.show({
              noBackdrop: true,
              template: "您的账号已在其他设备登录",
              duration: 1500
            });
            $state.go('login');
          }
          $rootScope.$broadcast('DeptByDivision.Updated');
        })
      },
      getResult:function(){
        return result;
      }
    }
  })
  .factory('District', function($resource, $rootScope,ENV,$ionicLoading,$state) {
    var ApiUrl = ENV.postUrl+ENV.api.getDistrict,
      provinceData,cityData,areaData;

    var resource = $resource(ApiUrl+':district',{},{
      query :{
        method : 'get',
        isArray : false
      }
    });
    return{
      getProvince : function(province){
        return resource.query({
          district:province
        },function(r){
          console.log(r);
          if (r.msg!="0003"){
            provinceData=r;
          }else {
            $ionicLoading.show({
              noBackdrop: true,
              template: "您的账号已在其他设备登录",
              duration: 1500
            });
            $state.go('login');
          }
          $rootScope.$broadcast('Province.Updated');
        })
      },
      getPro:function(){
        return provinceData;
      },
      getCity : function(city){
        return resource.query({
          district:city
        },function(r){
          console.log(r);
          cityData=r;
          $rootScope.$broadcast('City.Updated');
        })
      },
      getCit:function(){
        return cityData;
      },
      getArea : function(area){
        return resource.query({
          district:area
        },function(r){
          console.log(r);
          areaData=r;
          $rootScope.$broadcast('Area.Updated');
        })
      },
      getAre:function(){
        return areaData;
      }
    }
  })
